using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Web.UI.WebControls;

using dotnetCHARTING;

namespace hxSec.StaticPage
{
    /// <summary>
    /// 画饼图
    /// </summary>
    public class clsPieChrt
    {
        #region Fields
        // 图形数据源
        private DataTable _data;

        private int _width;         // 图形宽度
        private int _height;        // 图形高度 
        private string _filename;   // 图形文件名称
        private string _account;    // 客户号
        private string _format;     // 图形格式

        #endregion

        #region Contructors
        public clsPieChrt(int iWidth, int iHeight, string strAccount, string strFormat, DataTable dtData)
        {
            Data = dtData;

            Width = iWidth;
            Height = iHeight;
            Account = strAccount;
            ImgFormat = strFormat;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// 图形数据源
        /// </summary>
        public DataTable Data
        {
            get { return _data; }
            set { _data = value; }
        }

        /// <summary>
        /// 图形宽度
        /// </summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// 图形高度
        /// </summary>
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// 图形文件名称
        /// </summary>
        public string FileName
        {
            get { return _filename; }
            set { _filename = value; }
        }

        /// <summary>
        /// 客户号
        /// </summary>
        public string Account
        {
            get { return _account; }
            set { _account = value; }
        }

        /// <summary>
        /// 图形格式
        /// </summary>
        public string ImgFormat
        {
            get { return _format; }
            set { _format = value; }
        }
        #endregion

        #region DrawChart
        public byte[] DrawPieChart()
        {
            if (Data == null || Data.Rows.Count == 0)
                return new byte[0];

            dotnetCHARTING.Chart pieChart = new dotnetCHARTING.Chart();

            // Set the title.
            pieChart.Title = "";
            pieChart.LegendBox.Position = LegendBoxPosition.Middle;
            pieChart.LegendBox.Template = "%PercentOfTotal%icon%name%";
            pieChart.LegendBox.ClearColors();
            // Set 3D
            pieChart.Use3D = false;

            // Set the Chart1 Type
            pieChart.Type = ChartType.Donut;

            // Set default transparency
            pieChart.DefaultSeries.DefaultElement.Transparency = 1;

            // Set show value
            pieChart.DefaultSeries.DefaultElement.ShowValue = true;
            pieChart.YAxis.FormatString = "0.0";

            // Set labeling mode.
            pieChart.PieLabelMode = PieLabelMode.Inside;

            // Set the directory where the images will be stored.
            pieChart.TempDirectory = "";

            // Set he chart size.
            pieChart.Width = Unit.Pixel(Width);
            pieChart.Height = Unit.Pixel(Height); ;

            // Set background
            //Chart1.ChartArea.Background.Color = Color.White;
            pieChart.ChartArea.ClearColors();

            // Add the random data.
            pieChart.SeriesCollection.Add(getRandomData());

            Bitmap b = pieChart.GetChartBitmap();
            MemoryStream imgStream = new MemoryStream();

            switch(ImgFormat)
            {
                case "bmp":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case "jpg":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "gif":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                case "png":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                default:
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
            }
            

            return imgStream.ToArray();
        }

        SeriesCollection getRandomData()
        {
            SeriesCollection SC = new SeriesCollection();

            for (int a = 0; a < Data.Rows.Count; a++)
            {
                if (Convert.ToDouble(Data.Rows[a]["VALUE"]) > 0)
                {
                    Series s = new Series();
                    s.Name = Data.Rows[a]["NAME"].ToString();

                    Element e = new Element();
                    e.YValue = Convert.ToDouble(Data.Rows[a]["VALUE"]);
                    s.Elements.Add(e);

                    SC.Add(s);
                }
            }
            return SC;
        }
        #endregion
    }
}
